/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.pridexmltomgfconverter.playground;

import com.compomics.pridexmltomgfconverter.tools.MultipleOmicsPrideXMLParamExtractor;
import java.io.File;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;
import javax.swing.text.AbstractDocument;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class MutlipleOmicsRunner {

    private static File inputfolder;
    private static File outputfolder;
    private static Logger LOGGER = Logger.getLogger(MutlipleOmicsRunner.class);

    public static void main(String args[]) {
        Logger.getRootLogger().setLevel(Level.DEBUG);
        try {
            parseArgs(args);
        } catch (ParseException ex) {
            LOGGER.error(ex);
        }

        LOGGER.info("Checking for existing outputfiles...");
        MultipleOmicsPrideXMLParamExtractor instance = MultipleOmicsPrideXMLParamExtractor.getInstance();
        File[] outputtedFiles = outputfolder.listFiles();
        final List<File> myOutputFiles = Arrays.asList(outputtedFiles);
        File[] filesToAnalyze = inputfolder.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                boolean accepted = false;
                accepted = pathname.getAbsolutePath().toLowerCase().endsWith(".xml") || pathname.getAbsolutePath().toLowerCase().endsWith(".xml.gz");
                if (accepted) {
                    //parse the prideXML accession : 
                    String fileName = outputfolder.getAbsolutePath() + "/" + pathname.getName().substring(pathname.getName().lastIndexOf("_") + 1, pathname.getName().indexOf(".xml")) + ".txt";
                    LOGGER.debug(fileName);
                    accepted = !myOutputFiles.contains(new File(fileName));
                }
                return accepted;
            }
        });
        for (File aFile : filesToAnalyze) {
            try {
                if (aFile.length() < 1024 * 1024 * 1024) {
                    String fileName = aFile.getName();
                    instance.init(aFile);
                    try {
                        instance.makeReport(new File(outputfolder, fileName.substring(fileName.lastIndexOf("_") + 1, fileName.indexOf(".xml")) + ".txt"));
                    } catch (Exception ex) {
                        LOGGER.error(ex);
                    }
                } else {
                    LOGGER.error("File is too large (1GB+), skipping !");
                }
            } catch (Throwable e) {
                LOGGER.error(e);
            }
        }
    }

    public static void parseArgs(String[] args) throws ParseException {
        Options options = new Options() {
        };
        options.addOption("inputfolder", true, "folder with the files that need to be analyzed");
        options.addOption("outputfolder", true, "folder with the resulting files will be written to");

        CommandLineParser parser = new BasicParser();
        CommandLine cmd = parser.parse(options, args);
        if (cmd.hasOption("inputfolder")) {
            inputfolder = new File(cmd.getOptionValue("inputfolder"));
        }
        if (cmd.hasOption("outputfolder")) {
            outputfolder = new File(cmd.getOptionValue("outputfolder"));
        }
    }
}
